/**
 * Created by Administrator on 2017/4/18.
 */
var Countdown = require('../../../../src/libs/countdown');
var UFormatter = require('../../../../src/core/utils/formatter.js');

var InvestAction = React.createClass({
    getDefaultProps:function(){
        return {
            data:{
                minFlag:true,
                flag:{
                    login:true,
                    loginUrl:"",
                    confirmUrl:"",
                    ProjectStatus:1,
                    JMBeginTime:"",
                    ProjectBeginTime:"",

                    MinInvestPartsCount:"",
                    MaxInvestPartsCount:"",
                    EachPartAmount:""
                }
            }
        }
    },
    getInitialState:function(){
        return {
            data: this.props.data
        }
    },
    _clickHandle:function(){
        var jmTime = new Date(UFormatter.formatTime(this.state.data.flag.JMBeginTime,"yyyy/mm/dd hh:mm:ss")).getTime();
        if(this.state.data.flag.ProjectStatus == 3 || jmTime < new Date().getTime()){
            return;
        }

        var confirmUrl = this.state.data.flag.confirmUrl;
        if(typeof confirmUrl == 'function' && confirmUrl.constructor == Function){
            confirmUrl();
        }
    },
    _overFn:function(){
        // console.log('满标')
        this.setState({
            data:{
                flag:{
                    ProjectStatus:3
                }
            }
        });
        UEventHub.emit('countOver',{ProjectStatus:3});
    },
    _actionFn:function(){
        // console.log("马上投标")
        this.setState({
            data:{
                flag:{
                    ProjectStatus:1
                }
            }
        });
        UEventHub.emit('countOver',{ProjectStatus:1});
    },
    render:function(){
        var _this = this;
        var item = this.state.data.flag;
        if(item.login == undefined){
            item.login = true;
        }
        //按钮文案
        var arrBtn = [];
        if (this.state.data.minFlag) {
            arrBtn = [
                {
                    text:"投标中",
                    className:"btn-bidnow"
                },
                {
                    text:"预热中",
                    className:"btn-wait"
                },
                {
                    text:"已满标",
                    className:"btn-full"
                },
                {
                    text:"计息中",
                    className:"btn-full"
                },
                {
                    text:"回款中",
                    className:"btn-full"
                },
                {
                    text:"已还款",
                    className:"btn-full"
                }
            ];
        }
        else {
            arrBtn = [
                {
                    text: "投标中",
                    className: "btn-bidnow"
                },
                {
                    text: "预约投资",
                    className: "btn-wait"
                },
                {
                    text: "已满标",
                    className: "btn-disabled"
                },
                {
                    text: "计息中",
                    className: "btn-disabled"
                },
                {
                    text: "回款中",
                    className: "btn-disabled"
                },
                {
                    text: "已还款",
                    className: "btn-disabled"
                }
            ];
        }

        if(!this.state.data.minFlag){
            var jmTime = new Date(UFormatter.formatTime(this.state.data.flag.JMBeginTime,"yyyy/mm/dd hh:mm:ss")).getTime();
            if(jmTime < new Date().getTime()){
                arrBtn[2].className = "btn-disabled";
                arrBtn[0] = arrBtn[2];
            }else{
                arrBtn[0].text = item.login?"马上投资":"马上登录投资";
            }
        }else{
            arrBtn[0].text = "马上投资";
        }

        var _url = "";
        // if(item.login){
            if(item.confirmUrl && typeof item.confirmUrl == "string"){
                _url = item.confirmUrl;
            }else{
                _url = "javascript:void(0)";
            }
        // }else{
        //     _url = item.loginUrl;
        // }
        if(item.ProjectStatus){
            return(
                <div className={this.state.className}>
                    {this.state.data.minFlag ?
                        <a className={"btn "+arrBtn[item.ProjectStatus - 1].className} href={item.confirmUrl}>
                            {arrBtn[item.ProjectStatus - 1].text}
                        </a> :
                        <a className={"btn "+arrBtn[item.ProjectStatus - 1].className}
                           href={_url}
                           onClick={_this._clickHandle.bind(_this)}>
                            {arrBtn[item.ProjectStatus - 1].text}
                        </a>
                    }
                    {
                        item.ProjectStatus == 1 ?
                            <p>距项目结束还有：<Countdown timeRemaining={item.JMBeginTime} tpl="{d}天{h}小时{m}分{s}秒" overCallback={this._overFn.bind(this)}/></p> :
                            item.ProjectStatus == 2 ?
                                <p>距项目开始还有：<Countdown timeRemaining={item.ProjectBeginTime} tpl="{d}天{h}小时{m}分{s}秒" overCallback={this._actionFn.bind(this)}/></p> : ""
                    }
                </div>
            )
        }else{
            return <div></div>
        }
    }
});
module.exports = InvestAction;